package me.zhengjie.modules.xiao.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.xiao.domain.CgNotice;
import me.zhengjie.modules.xiao.domain.vo.CgNoticeQueryCriteria;
import me.zhengjie.modules.xiao.service.CgNoticeService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author CY
* @date 2024-07-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "通知管理")
@RequestMapping("/api/cgNotice")
public class CgNoticeController {

    private final CgNoticeService cgNoticeService;
//    private final UserService userService;
//    private final DeptService deptService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('cgNotice:list')")
    public void exportCgNotice(HttpServletResponse response, CgNoticeQueryCriteria criteria) throws IOException {
        cgNoticeService.download(cgNoticeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询通知")
    @ApiOperation("查询通知")
    public ResponseEntity<PageResult<CgNotice>> queryCgNotice(CgNoticeQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(cgNoticeService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增通知")
    @ApiOperation("新增通知")
    public ResponseEntity<Object> createCgNotice(@Validated @RequestBody CgNotice resources){
        cgNoticeService.create(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @Log("修改通知")
    @ApiOperation("修改通知")
    public ResponseEntity<Object> updateCgNotice(@Validated @RequestBody CgNotice resources){
        cgNoticeService.update(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除通知")
    @ApiOperation("删除通知")
    public ResponseEntity<Object> deleteCgNotice(@RequestBody List<Integer> ids) {
        cgNoticeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/status/{id}")
    @Log("已读")
    @ApiOperation("已读")
    public ResponseEntity<Object> findCgCluetable(@PathVariable("id") Long id){
        CgNotice cgNotice = cgNoticeService.getById(id);
        cgNotice.setStatus("2");
        cgNoticeService.update(cgNotice);
        return new ResponseEntity<>(cgNotice,HttpStatus.OK);
    }
}